<?php
/**
 * Created by PhpStorm.
 * User: Love
 * Date: 2018/8/8
 * Time: 14:17
 */

namespace App\Extend;


use App\Area;

class Tools
{
    protected static $str = 'qwertyuiopasdfghjklzxcvbnm123456789QWERTYUIOPASDFGHJKLZXCVBNM';

    public static function getRenderChar(int $len=16)
    {
        $str = '';
        for ($i=0;$i<$len;$i++){
            $str.= self::$str[mt_rand(0, strlen(self::$str) - 1)];
        }
        return $str;
    }

    public static function getRenderTimeChar(int $len=16)
    {
        return substr(time(),5,-1).self::getRenderChar($len-6);
    }

    public static function getMaskPhone(string $phone){
        return substr($phone, 0, 3).'****'.substr($phone, 7);
    }

    //设置省市区格式
    public static function genCode($province,$city,$area)
    {
        if($province == 0 ) {
            return [];
            $province = Area::findOrFail($province)->toArray();
        }
        if($city == 0 ) {
            return [
                [
                'name'=>$province['name'],
                'code'=>$province['id']
                ]
            ];
            $city = Area::findOrFail($city)->toArray();
        }
        if($area == 0) {
            return [
                [
                'name'=>$province['name'],
                'code'=>$province['id']
                ],
               [
                   'name'=>$city['name'],
                   'code'=>$city['id']
               ]
            ];
        }
        
        $area = Area::findOrFail($area)->toArray();
        return [
            [
                'name'=>$province['name'],
                'code'=>$province['id']
            ],
           [
               'name'=>$city['name'],
               'code'=>$city['id']
           ],
            [
                'name'=>$area['name'],
                'code'=>$area['id']
            ]
        ];
    }
}